/**
 * @class   wosh::gui::GuiUtilities
 * @brief   Provides some common functions.
 *          
 *
 ****************************************************************************
 * @version $Id: GuiUtilities.h 2883 2010-08-09 14:51:56Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_Utilities_H__
 #define __WOSH_Gui_Utilities_H__

 #include <woshDefs.h>
 #include <core/ObjectAllocator.h>
 #include <core/Containers.h>

 #include <framework/communication/SessionProxy.h>

 #include <framework/gui/IWidget.h>
 #include <framework/gui/IWidgetSerializable.h>

 #include <string>

 #include <QtGui/QIcon>
 #include <QVariant>
 #include <QStringList>
class QWidget;

namespace wosh {
 class Response;
 namespace gui {

#undef WOSH_GUI_MAP
#define WOSH_GUI_MAP( GUI_CLASSNAME, CLASSNAME, VERSION, TMP ) \
	GuiMapperT<GUI_CLASSNAME> my_gui_map_##TMP(CLASSNAME, VERSION); 


class GuiMappings
 {
	public:
		GuiMappings() { }

		 // meyer singleton
		static MapT<std::string, ObjectTypeInfo>& getMappings() {
			static MapT<std::string, ObjectTypeInfo> mappings;
			return mappings;
		 }

 };

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

template <typename GuiObjectType>
class GuiMapperT
 {
	public:
		GuiMapperT( const char* classname, double current_version = 0.0 ) {
			registerMySelf(classname, current_version);
		}

		virtual ~GuiMapperT() { }

	protected:
		void registerMySelf( const char* classname, double current_version = 0.0 ) {
			if ( classname == NULL || strlen(classname) == 0 ) return;
			ObjectTypeInfo info;
			info.name = GuiObjectType::className();
			info.version = current_version;
			info.parent = "";
			info.allocable = true;
			GuiMappings::getMappings().transactionBeginWrite();
			GuiMappings::getMappings().set( classname, info);
			GuiMappings::getMappings().transactionEnd();
		 }

}; // class def

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

class GuiSession : public wosh::communication::SessionProxy
 {
	public:
		GuiSession();
		~GuiSession();

	public:
		using Session::login;
		using Session::setUsernameLogged;
		using Session::setCredentials;

	public:
		std::string getCurrentKernel() const	{ return getCurrentLocation().getKernelName(); }

		using Session::getCredentials;

	public:
		SecurityToken* createToken( const Object* creator = NULL);
		SecurityToken* signMessage( Message* message );
		SecurityToken* signMessage( const Object* creator, Message* message );

	public:
		 // meyer singleton
		static GuiSession& getInstance() {
			static GuiSession gui_session;
			return gui_session;
		 }

}; // class def

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

class GuiUtilities
 {
	public:
		static IWidget* create_GUI( const std::string& name, const std::string& type, const URI& uri, const QStringList& interfaces );

		static IReflection* createGuiFor( const std::string& type, double version = 0.0 );

		template<class DYNAMIC_CAST>
		static DYNAMIC_CAST* createGuiFor( const std::string& type, double version = 0.0 ) {
			return dynamic_cast<DYNAMIC_CAST*>( createGuiFor(type, version) );
		 }

		static ObjectTypeInfo findGuiFor( const std::string& type, double version = 0.0, bool neareast = true );

		static QIcon createIconFor( const std::string& objType, bool load_nearest = true );

		static void list_GUI( std::vector<std::string>& types );


	public:
		typedef void (*WIDGET_SHOW)(QWidget*);
		static void setShowWidgetMethod( WIDGET_SHOW fptr );

		static void showWidget( QWidget* widget );

	public:
		static QVariant convertSerializable2QVariant( const wosh::ISerializable* object );
		static QVariant convertVariant2QVariant( const wosh::Variant* dataObject, Variant::DATA_TYPE default_type = Variant::EMPTY );
		static void convertQVariant2Variant( wosh::Variant* dataObject, const QVariant& varData );

	public:
		static void promptMethodResponse( const Response* response, const QString& parent_title );
		static void aboutWOSH( QWidget* parent );
		static void aboutError_woshconf( QWidget* parent, const std::string& cfgFile );
		static bool warning( const QString& title, const QString& text );


	protected:
		GuiUtilities() { }
		virtual ~GuiUtilities() { }


}; // class def

 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Gui_Utilities_H__
